%Goal: mortgage market equilibrium

%INPUTS:
%alpha: household price sensitivity
%F: total mortgage face value
%Nb: number of local banks
%No: number of non-local banks
%Nn: number of shadow banks
%qn: shadow bank mortgage quality 
%qb: bank mortgage quality
%rho0: bank funding cost
%rhon: common component of shadow bank funding cost
%eta: warehouse duration
%sigma_xi: volatility of shadow bank idiosyncratic taste shock
%co: non-local bank marginal cost of warehouse lending
%cb: local bank marginal cost of warehouse lending
%shadow bank idiosyncratic taste shock






function [rno, sno, rnbl,snbl,rnb,snb,rm,sbm,rbl,sbl,rb,sb] = Solve_profit_wbank_other(psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo,rhob,rhobl,sow,sbw)

MaxIterations=10000;
tol = 10^(-9);
rb_initial = 1.85;
rb = [];
diff = 1000;
iter = 0;

while diff>tol && iter<=MaxIterations  
        iter = iter+1;
        [rno, sno, rnbl,snbl,rnb,snb,rm,sbm,rbl,sbl] = Solve_profit_wbank(psim,psiw,alpha,k,Nb,Nn,Nm,qn,qb,eta,rhoo,rhob,rhobl,sow,sbw,rb_initial);
        sb = exp(-alpha*rb_initial+qb)/(1+exp(-alpha*rbl+qb)+(Nb-1)*exp(-alpha*rb_initial+qb)+Nm*exp(-alpha*rm+qb)+Nn*sow*exp(-alpha*rno+qn)+Nn*sbw*exp(-alpha*rnbl+qn)+Nn*sbw*(Nb-1)*exp(-alpha*rnb+qn));
        rb = psim+1/(alpha*(1-sb))+eta*(rhob-psiw)*Nn*sbw*snb/(1-sb);
        diff = abs(rb-rb_initial);
        rb_initial = rb;    
  
end 

end





